package com.lab.system.service.impl;

import com.lab.common.core.domain.entity.SysUser;
import com.lab.system.domain.ExperimentCourse;
import com.lab.system.domain.Feedback;
import com.lab.system.domain.Reservations;
import com.lab.system.mapper.FeedbackMapper;
import com.lab.system.service.IExperimentCourseService;
import com.lab.system.service.IFeedbackService;
import com.lab.system.service.IReservationsService;
import com.lab.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 实验反馈Service业务层处理
 *
 * @author cf
 * @date 2025-02-15
 */
@Service
public class FeedbackServiceImpl implements IFeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IReservationsService reservationService;

    @Autowired
    private IExperimentCourseService courseService;

    /**
     * 查询实验反馈
     *
     * @param id 实验反馈主键
     * @return 实验反馈
     */
    @Override
    public Feedback selectFeedbackById(Long id) {
        return feedbackMapper.selectFeedbackById(id);
    }

    /**
     * 查询实验反馈列表
     *
     * @param feedback 实验反馈
     * @return 实验反馈
     */
    @Override
    public List<Feedback> selectFeedbackList(Feedback feedback) {
        List<Feedback> feedbacks = feedbackMapper.selectFeedbackList(feedback);
        List<Long> userIds = feedbacks.stream().map(Feedback::getUserId).collect(Collectors.toList());
        List<Long> reservationIds = feedbacks.stream().map(Feedback::getReservationId).collect(Collectors.toList());
        List<SysUser> sysUsers = userService.queryByUserIds(userIds);
        List<Reservations> reservationsList = reservationService.selectReservationsByIds(reservationIds);
        for (Reservations reservations : reservationsList) {
            ExperimentCourse experimentCourse = courseService.selectExperimentCourseById(reservations.getCourseId());
            if (Objects.nonNull(experimentCourse)) {
                reservations.setCourseName(experimentCourse.getCourseName());
            }
        }
        Map<Long, Reservations> reservationsMap = reservationsList.stream().collect(Collectors.toMap(Reservations::getId, reservations -> reservations));
        Map<Long, SysUser> userMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, sysUser -> sysUser));
        feedbacks.forEach(x -> {
            x.setUserName(userMap.get(x.getUserId()).getUserName());
            x.setCourseName(reservationsMap.get(x.getReservationId()).getCourseName());
        });
        return feedbacks;
    }

    /**
     * 新增实验反馈
     *
     * @param feedback 实验反馈
     * @return 结果
     */
    @Override
    public int insertFeedback(Feedback feedback) {
        return feedbackMapper.insertFeedback(feedback);
    }

    /**
     * 修改实验反馈
     *
     * @param feedback 实验反馈
     * @return 结果
     */
    @Override
    public int updateFeedback(Feedback feedback) {
        return feedbackMapper.updateFeedback(feedback);
    }

    /**
     * 批量删除实验反馈
     *
     * @param ids 需要删除的实验反馈主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackByIds(Long[] ids) {
        return feedbackMapper.deleteFeedbackByIds(ids);
    }

    /**
     * 删除实验反馈信息
     *
     * @param id 实验反馈主键
     * @return 结果
     */
    @Override
    public int deleteFeedbackById(Long id) {
        return feedbackMapper.deleteFeedbackById(id);
    }
}
